<template>
  <div id="chartInstance" style="width: 95%; height: 300px"></div>
</template>
<script>
import * as echarts from 'echarts';

export default {
  data() {
    return {
        colorArr: [
          ["rgba(26, 195, 174, 1)", "rgba(30, 198, 149, 1)"],
          ["rgba(26, 195, 174, 1)", "rgba(30, 198, 149, 1)"],
          ["rgba(26, 195, 174, 1)", "rgba(30, 198, 149, 1)"],
        ],
      // 图表的实例对象
      chartInstance: null,
      provinceInfo: [
        "通知",
        "公告",
        "活动",
        "家园任务",
        "缴费通知",
        "选课通知",
        "作业",
        "请假",
        "发布相册",
        "评价",
        "投票",
        "带药提醒",
      ],
      valueArr: [
        100,
        200,
        300,
        400,
        123,
        234,
        345,
        456,
        124,
        241,
        134,
        241,
      ],
 
      // 柱形图 区域缩放起点值
      startValue: 0,
      // 柱形图结 区域缩放终点值
      endValue: 9,
      // 定时器
      timerId: null
    };
  },
  mounted() {
    this.drawChart();
    this.updateChart();
    this.startInterval();
  },
  destroyed() {
    clearInterval(this.timerId);
  },
  methods: {
    drawChart() {
      this.chartInstance = echarts.init(
        document.getElementById("chartInstance")
      );
      this.chartInstance.setOption({
        title: {
          text: "消息发布数量统计",
          left: 20,
          top: 20
        },
        grid: {
          top: "40%",
          left: "5%",
          right: "5%",
          bottom: "5%",
          // 把x轴和y轴纳入 grid
          containLabel: true
        },
        tooltip: {
          show: true
        },
        xAxis: {
          type: "category"
        },
        yAxis: {
          value: "value"
        },
        series: [
          {
            type: "bar",
            label: {
              show: true,
              position: "top",
              color: "#000",
              rotate: 30
            },
            barWidth: 30,
            itemStyle: {
              barBorderRadius: [10, 10, 0, 0] //[左上角，右上角，左小角，右下角]
            }
          }
        ]
      });
 
      // 鼠标经过关闭 动画效果
      this.chartInstance.on("mouseover", () => {
        clearInterval(this.timerId);
      });
      // 鼠标离开 开启动画效果
      this.chartInstance.on("mouseout", () => {
        this.startInterval();
      });
    },
    // 更新图表配置项
    updateChart() {

      const dataOption = {
        xAxis: {
          data: this.provinceInfo
        },
        dataZoom: {
          // 区域缩放组件
          show: false,
          startValue: this.startValue,
          endValue: this.endValue
        },
        series: [
          {
            data: this.valueArr,
            itemStyle: {
              shadowColor: "#5470c6",
              shadowBlur: 3,
              color: arg => {
                let targetColorArr = null;
 
                if (arg.value > 300) {
                  targetColorArr = this.colorArr[0];
                } else if (arg.value > 200) {
                  targetColorArr = this.colorArr[1];
                } else {
                  targetColorArr = this.colorArr[2];
                }
 
                return new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  // 0%
                  { offset: 0, color: targetColorArr[0] },
                  // 100%
                  { offset: 1, color: targetColorArr[1] }
                ]);
              }
            }
          }
        ]
      };
      this.chartInstance.setOption(dataOption);
    },
    // 改变柱形图 区域缩放起始与终点值的函数
    startInterval() {
      // 如果存在则关闭
      this.timerId && clearInterval(this.timerId);
 
      this.timerId = setInterval(() => {
        this.startValue++;
        this.endValue++;
        if (this.endValue > this.provinceInfo.length - 1) {
          this.startValue = 0;
          this.endValue = 9;
        }
        this.updateChart();
      }, 2000);
    }
  }
};
</script>
 
<style scoped>
#chartInstance {
  background-color: #fff;
  border-radius: 10px ;
  box-shadow: 0 8px 12px #ebedf0;
  margin: auto;
 }
</style>